#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
    int data;
    struct ListNode* next;
};
struct ListNode* detectCycle(struct ListNode* head)
{
    struct ListNode* slow = head;
    struct ListNode* fast = head;
    struct ListNode* meet = NULL;
    while (fast != NULL && fast->next != NULL)
    {
        if (fast == slow)
        {
            meet = fast;
            break;
        }
        slow = slow->next;
        fast = fast->next->next;
    }
    if (fast == NULL || fast->next == NULL)
    {
        return NULL;
    }
    struct ListNode* cur1 = head;
    struct ListNode* cur2 = meet;
    while (cur1 != cur2)
    {
        cur1 = cur1->next;
        cur2 = cur2->next;
    }
    return cur1;
}

int main()
{
    struct ListNode* n1 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n2 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n3 = (struct ListNode*)malloc(sizeof(struct ListNode));
    struct ListNode* n4 = (struct ListNode*)malloc(sizeof(struct ListNode));
    n1->data = 3;
    n2->data = 2;
    n3->data = 0;
    n4->data = -4;
    n1->next = n2;
    n2->next = n3;
    n3->next = n4;
    n4->next = n2;
    struct ListNode* pos = detectCycle(n1);
    printf("%d", pos->data);
	return 0;
}